/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiaDetalles.java
 *
 * Created on 01/03/2011, 06:50:34 PM
 */
package com.catosist.run.application.venta.guia;

import com.catosist.run.business.gestorComprobanteDetalle.GestorComprobanteDetalle;
import com.catosist.run.business.gestorguia.GestorGuia;
import com.catosist.run.services.ComprobanteDetalleDTO;
import com.catosist.run.services.GuiaDTO;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.springframework.dao.DataIntegrityViolationException;

/**
 *
 * @author igorov
 */
public class GuiaDetalles extends javax.swing.JFrame {

    /** Creates new form GuiaDetalles */
    public GuiaDetalles(ManGuia manGuia, GestorGuia gestorGuia, int guiaId, GestorComprobanteDetalle gestorComprobanteDetalle) {
        initComponents();
        this.manGuia = manGuia;
        this.lista_comprobantes_con_guias = manGuia.getLista_comprobantes_con_guias();
        this.lista_comprobantes_sin_guias = manGuia.getLista_comprobantes_sin_guias();
        this.gestorComprobanteDetalle = gestorComprobanteDetalle;
//        this.lista_comprobantes_agregados = new LinkedList<ComprobanteDetalleDTO>();
        this.gestorGuia = gestorGuia;
        this.guiaId = guiaId;
        map_ids_comprobantedetalles = new HashMap<Integer, Integer>();

        refresh();
    }

    public JPanel getPanel() {
        return jPanel1;
    }

    private void guardar_guia() {
        System.out.print("FLAG?");
        GuiaDTO guiaDTO = new GuiaDTO();
        guiaDTO.setDescripcion(txtDescripcion.getText());
        guiaDTO.setDestinatariodireccion(txtDestinatarioDireccion.getText());
        guiaDTO.setDestinatarionombre(txtDestinatarioNombre.getText());
        guiaDTO.setNumero(txtNumero.getText());
        guiaDTO.setRemitentedireccion(txtRemitenteDireccion.getText());
        guiaDTO.setRemitentenombre(txtRemitenteNombre.getText());
        guiaDTO.setId(guiaId);

        gestorGuia.saveOrUpdate(guiaDTO);

        if (guiaId == 0) {
            guiaId = gestorGuia.getIdByNumero(guiaDTO.getNumero());
        }
    }

    private void refresh() {
        model = new DefaultTableModel();
        model.addColumn("Id");
        model.addColumn("Cantidad");
        model.addColumn("Producto");
        jTable1.setModel(model);

        //Llena una lista comprobantedetalles que no que no estan en ninguna guia
        List<String> productos = new LinkedList<String>();
        for (int i = 0; i < lista_comprobantes_sin_guias.size(); i++) {
//            if(lista_comprobantes.get(i).getGuiaId() == -1){
            //Inserta el comprobantedetalle que no corresponde a ninguna
            //guia a la lista productos
            productos.add(lista_comprobantes_sin_guias.get(i).getCantidad() + ", " + lista_comprobantes_sin_guias.get(i).getProducto());
            //Llena un mapa con clave el indice de cada item del combo,
            //y como valor el id de su correspondiente comprobantedetalle
            map_ids_comprobantedetalles.put(productos.indexOf(lista_comprobantes_sin_guias.get(i).getCantidad() + ", " + lista_comprobantes_sin_guias.get(i).getProducto()), lista_comprobantes_sin_guias.get(i).getId());
//            }
        }
        //Se inserta en el combo esos productodetalles que estan sin guias
        cmbProducto.setModel(new DefaultComboBoxModel(productos.toArray()));

        //Agrega al modelo para la grilla, los comprobantedetalles que corresponden
        //a la guia seleccionada
        for (Iterator<ComprobanteDetalleDTO> it = lista_comprobantes_con_guias.iterator(); it.hasNext();) {
            ComprobanteDetalleDTO comprobanteDetalleDTO = it.next();
            if (comprobanteDetalleDTO.getGuiaId() == guiaId) {
                model.addRow(comprobanteDetalleDTO.toArray2());
            }
        }
        this.jTable1.setModel(model);

        //Si se trata de la edicion de una guia, se llenaran sus campos
        if (guiaId != 0) {
            GuiaDTO guiaDTO = (GuiaDTO) gestorGuia.getById(guiaId);
            txtDescripcion.setText(guiaDTO.getDescripcion());
            txtNumero.setText(String.valueOf(guiaDTO.getNumero()));
            txtRemitenteNombre.setText(guiaDTO.getRemitentenombre());
            txtRemitenteDireccion.setText(guiaDTO.getRemitentedireccion());
            txtDestinatarioNombre.setText(guiaDTO.getDestinatarionombre());
            txtDestinatarioDireccion.setText(guiaDTO.getDestinatariodireccion());
        }

    }

    private boolean comprobarCampos() {
        if (txtDescripcion.getText().length() != 0
                && txtDestinatarioDireccion.getText().length() != 0
                && txtDestinatarioNombre.getText().length() != 0
                && txtNumero.getText().length() != 0
                && txtRemitenteDireccion.getText().length() != 0
                && txtRemitenteNombre.getText().length() != 0) {
            return true;
        } else {
            return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNumero = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtRemitenteNombre = new javax.swing.JTextField();
        txtRemitenteDireccion = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        txtDestinatarioNombre = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtDestinatarioDireccion = new javax.swing.JTextField();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        cmbProducto = new javax.swing.JComboBox();
        btnAgregarProducto = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnEliminarProducto = new javax.swing.JButton();
        btnAgregarTodos = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.catosist.run.application.desktopapplication.DesktopApplication.class).getContext().getResourceMap(GuiaDetalles.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtNumero.setText(resourceMap.getString("txtNumero.text")); // NOI18N
        txtNumero.setName("txtNumero"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtDescripcion.setText(resourceMap.getString("txtDescripcion.text")); // NOI18N
        txtDescripcion.setName("txtDescripcion"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel4.setForeground(resourceMap.getColor("jLabel4.foreground")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(resourceMap.getFont("jLabel5.font")); // NOI18N
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        txtRemitenteNombre.setText(resourceMap.getString("txtRemitenteNombre.text")); // NOI18N
        txtRemitenteNombre.setName("txtRemitenteNombre"); // NOI18N

        txtRemitenteDireccion.setText(resourceMap.getString("txtRemitenteDireccion.text")); // NOI18N
        txtRemitenteDireccion.setName("txtRemitenteDireccion"); // NOI18N

        jLabel7.setForeground(resourceMap.getColor("jLabel7.foreground")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jLabel8.setFont(resourceMap.getFont("jLabel8.font")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        txtDestinatarioNombre.setName("txtDestinatarioNombre"); // NOI18N

        jLabel9.setFont(resourceMap.getFont("jLabel9.font")); // NOI18N
        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        txtDestinatarioDireccion.setName("txtDestinatarioDireccion"); // NOI18N

        jSeparator3.setName("jSeparator3"); // NOI18N

        jLabel10.setFont(resourceMap.getFont("jLabel10.font")); // NOI18N
        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        cmbProducto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbProducto.setName("cmbProducto"); // NOI18N

        btnAgregarProducto.setText(resourceMap.getString("btnAgregarProducto.text")); // NOI18N
        btnAgregarProducto.setName("btnAgregarProducto"); // NOI18N
        btnAgregarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarProductoActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        btnEliminarProducto.setText(resourceMap.getString("btnEliminarProducto.text")); // NOI18N
        btnEliminarProducto.setName("btnEliminarProducto"); // NOI18N
        btnEliminarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarProductoActionPerformed(evt);
            }
        });

        btnAgregarTodos.setText(resourceMap.getString("btnAgregarTodos.text")); // NOI18N
        btnAgregarTodos.setEnabled(false);
        btnAgregarTodos.setName("btnAgregarTodos"); // NOI18N
        btnAgregarTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(40, 40, 40)
                                .addComponent(btnAgregarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnAgregarTodos))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSeparator1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(26, 26, 26)
                                .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRemitenteNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtRemitenteDireccion))
                            .addComponent(jSeparator3)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSeparator2))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(20, 20, 20)
                                .addComponent(txtDestinatarioNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDestinatarioDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(282, 282, 282)
                        .addComponent(btnEliminarProducto)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtRemitenteNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtRemitenteDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtDestinatarioNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(txtDestinatarioDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(cmbProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAgregarProducto)
                    .addComponent(btnAgregarTodos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEliminarProducto)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarProductoActionPerformed
        // TODO add your handling code here:
        if (comprobarCampos()) {
            try {
                System.out.println("GUARDANDO LA GUIA");
                guardar_guia();
                int i = cmbProducto.getSelectedIndex();
                for (int j = 0; j < lista_comprobantes_sin_guias.size(); j++) {
                    if (lista_comprobantes_sin_guias.get(j).getId() == map_ids_comprobantedetalles.get(i)) {
                        lista_comprobantes_sin_guias.get(j).setGuiaId(guiaId);
                        lista_comprobantes_con_guias.add(lista_comprobantes_sin_guias.get(j));
                        gestorComprobanteDetalle.saveOrUpdate(lista_comprobantes_sin_guias.get(j), lista_comprobantes_sin_guias.get(j).getComprobanteId());
                        lista_comprobantes_sin_guias.remove(j);
                        break;
                    }
                }
                refresh();
                manGuia.refresh();
//            lista_comprobantes.get(map_ids_comprobantedetalles.get(i))


            }catch (DataIntegrityViolationException e1) {
                JOptionPane.showMessageDialog(this, "Ya existe una guia de remision con ese numero", "Advertencia", JOptionPane.WARNING_MESSAGE);
            }
            catch (Exception e) {
                System.out.println("Excepcion controlada " + e.toString());
                JOptionPane.showMessageDialog(this, "No hay mas productos", "Advertencia", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Debe llenar todos los datos de la guia", "Advertencia", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnAgregarProductoActionPerformed

    private void btnEliminarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarProductoActionPerformed
        // TODO add your handling code here:
        try {
            int ind = Integer.parseInt(this.jTable1.getValueAt(this.jTable1.getSelectedRow(), 0).toString());
            System.out.println("Id: " + ind);
//            for (Iterator<ComprobanteDetalleDTO> it = lista_comprobantes_con_guias.iterator(); it.hasNext();) {
//                ComprobanteDetalleDTO comprobanteDetalleDTO = it.next();
//                if (comprobanteDetalleDTO.getId() == indice) {
//                    comprobanteDetalleDTO.setGuiaId(-1);
//                    lista_comprobantes_sin_guias.add(comprobanteDetalleDTO);
//                    lista_comprobantes_con_guias.remove(indice)
//                }
//
//            }

            for (int i = 0; i < lista_comprobantes_con_guias.size(); i++) {
                ComprobanteDetalleDTO comprobanteDetalleDTO = lista_comprobantes_con_guias.get(i);
                System.out.println("Com det: " + comprobanteDetalleDTO.getProducto() + " it" + comprobanteDetalleDTO.getId());
//                System.out.println("Id para comparar: " + map_ids_comprobantedetalles.get(indice)+" con "+comprobanteDetalleDTO.getId());
                if (comprobanteDetalleDTO.getId() == ind) {
                    System.out.println("Encontrado: " + comprobanteDetalleDTO.getProducto()+","+comprobanteDetalleDTO.getComprobanteId());
                    comprobanteDetalleDTO.setGuiaId(0);
                    gestorComprobanteDetalle.saveOrUpdate(comprobanteDetalleDTO, comprobanteDetalleDTO.getComprobanteId());
                    lista_comprobantes_sin_guias.add(comprobanteDetalleDTO);
                    lista_comprobantes_con_guias.remove(i);
                }
                refresh();

            }
//            int i = Integer.parseInt(this.jTable1.getValueAt(indice,0).toString());


        } catch (Exception e) {
        }
    }//GEN-LAST:event_btnEliminarProductoActionPerformed

    private void btnAgregarTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarTodosActionPerformed
        // TODO add your handling code here:
        if (comprobarCampos()) {
            try {
                guardar_guia();
                System.out.println("cantidad de guias: "+lista_comprobantes_sin_guias.size());
//                for (int i = 0; i < cmbProducto.getItemCount(); i++) {
                for (int j = 0; j < lista_comprobantes_sin_guias.size(); j++) {
//                    System.out.println("comprobantes sin guias " + lista_comprobantes_sin_guias.get(j).getComprobanteId());
                    System.out.println("Comprobantedetalle:"+lista_comprobantes_sin_guias.get(j).getProducto()+","+guiaId);
                    lista_comprobantes_sin_guias.get(j).setGuiaId(guiaId);
                    lista_comprobantes_con_guias.add(lista_comprobantes_sin_guias.get(j));
                    gestorComprobanteDetalle.saveOrUpdate(lista_comprobantes_sin_guias.get(j), lista_comprobantes_sin_guias.get(j).getComprobanteId());
                    lista_comprobantes_sin_guias.remove(j);
                }


//                lista_comprobantes_sin_guias.removeAll(lista_comprobantes_sin_guias);
                refresh();
                manGuia.refresh();

            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "No hay mas productos", "Advertencia", JOptionPane.WARNING_MESSAGE);
            }
        }

    }//GEN-LAST:event_btnAgregarTodosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new GuiaDetalles().setVisible(true);
//            }
//        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarProducto;
    private javax.swing.JButton btnAgregarTodos;
    private javax.swing.JButton btnEliminarProducto;
    private javax.swing.JComboBox cmbProducto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtDescripcion;
    private javax.swing.JTextField txtDestinatarioDireccion;
    private javax.swing.JTextField txtDestinatarioNombre;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JTextField txtRemitenteDireccion;
    private javax.swing.JTextField txtRemitenteNombre;
    // End of variables declaration//GEN-END:variables
    private List<ComprobanteDetalleDTO> lista_comprobantes_con_guias;
    private List<ComprobanteDetalleDTO> lista_comprobantes_sin_guias;
//    private List<ComprobanteDetalleDTO> lista_comprobantes_agregados;
    private DefaultTableModel model;
    private GestorComprobanteDetalle gestorComprobanteDetalle;
    private GestorGuia gestorGuia;
    private int guiaId;
    private Map<Integer, Integer> map_ids_comprobantedetalles;
    private ManGuia manGuia;
}
